//
//  SmallStrategyViewController.m
//  Qyer
//
//  Created by 赵宇 on 15/9/16.
//  Copyright (c) 2015年 赵宇. All rights reserved.
//

#import "SmallStrategyViewController.h"
#import "BaseHeader.h"
@interface SmallStrategyViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation SmallStrategyViewController
-(void)dealloc
{
    [_url release];
    [_inforArr release];
    [_table release];
    [super dealloc];
}
-(void)viewWillAppear:(BOOL)animated
{
//    if (_table == nil) {
//        
//        [self creatTableview];
//    }
//    [_table reloadData];
    self.navigationController.navigationBarHidden = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
     _inforArr = [[NSMutableArray alloc]init];
    self.page = 1;
    [self getTheData:1];
    [self creatNav];
}
-(void)creatNav
{
    UIView *navV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    navV.backgroundColor = [UIColor colorWithRed:250./255 green:250./255 blue:250./255 alpha:1];
    UILabel *titLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 25, SCREEN_WIDTH, 30)];
    titLab.text = @"全部微锦囊";
    titLab.textAlignment = YES;
    [navV addSubview:titLab];
    
    UIButton *backToBut = [UIButton buttonWithType:UIButtonTypeCustom];
    backToBut.frame = CGRectMake(10, 25, 30, 30);
    [backToBut setImage:[UIImage imageNamed:@"iconfont-31fanhui1"] forState:UIControlStateNormal];
    [backToBut addTarget:self action:@selector(goBackToBefore) forControlEvents:UIControlEventTouchUpInside];
    [navV addSubview:backToBut];
    [self.view addSubview:navV];
    
    [navV release];
    [titLab release];
    
}
-(void)goBackToBefore
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 获取网络数据

-(void)getTheData:(int)page
{
      NSString *string = [NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/mguide_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&id=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&page=%d&track_app_version=6.6.6&track_user_id=6695170&type=city&v=1",self.url,page];
    [AFNblock getdataWithUrlString:string dataBlock:^(id block) {
        [self dealDataWithDic:block];
        
    } netBlock:^(id block) {
        
        [self dealDataWithDic:block];
    }];
}
-(void)dealDataWithDic:(NSDictionary *)block
{
    NSDictionary *bigDic = block;
    NSArray *dataArr = [bigDic objectForKey:@"data"];
    for (int i = 0; i < dataArr.count; i++) {
        NSDictionary *dic = [dataArr objectAtIndex:i];
        SmallStrategyModel *small = [[SmallStrategyModel alloc]initWithDictionary:dic];
        [_inforArr addObject:small];
    }
    if (_table == nil) {
        
        [self creatTableview];
    }
    [_table reloadData];
    [_table.footer endRefreshing];

    if (_inforArr.count == 0) {
        
        UIImageView *noView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        [noView setImage:[UIImage imageNamed:@"no"]];
        [self.view addSubview:noView];
        
        
    }
    
}
#pragma mark - 创建Tableview
-(void)creatTableview
{
    if (_inforArr.count != 0) {
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64,SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        [_table setDelegate:self];
        [_table setDataSource:self];
        [self.view addSubview:_table];
        [self shangla];
        
        [_table release];
    }
}
#pragma mark tableview的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _inforArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *smallcellId = @"smallcellID";
    SmallStrategyTableViewCell *smallcell = [tableView dequeueReusableCellWithIdentifier:smallcellId];
    if (!smallcell) {
        smallcell = [[SmallStrategyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:smallcellId];
    }
    SmallStrategyModel *smallMol = [_inforArr objectAtIndex:indexPath.row];
    [smallcell getValueWithModel:smallMol];
    return smallcell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT/2;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SmallStrategyModel *mol = [_inforArr objectAtIndex:indexPath.row];
    ThemeViewController *theme = [[ThemeViewController alloc]init];
    theme.url = mol.cid;
    [self.navigationController pushViewController:theme animated:YES];
    
}
-(void)shangla
{
    __block SmallStrategyViewController *sma = self;
    [_table addLegendFooterWithRefreshingBlock:^{
        sma.page += 1;
        [sma getTheData:sma.page];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
